    ORG 0

; 常量
MOV DPTR, #SEGTABLE

MAIN:
    MOV R0, #0
    MOV R1, #0
    MOV R2, #0
    MOV R3, #0

    MOV R4, #0

    MOV TMOD, #01H
    MOV TH0, #3CH
    MOV TH1, #0B0H
    SETB TR0

MAIN_LOOP:
    LCALL printing
    JBC TF0, CLOCK
    JMP MAIN_LOOP
CLOCK:
    MOV TH0, #3CH
    MOV TH1, #0B0H
    INC R4
    
    CJNE R4, #2, NOT_YET_CLOCK
    
    REAL_CLOCK:     ; 够 100 ms 了
        MOV R4, #0
        INC R0
        LCALL formating

    NOT_YET_CLOCK:
        
    JMP MAIN_LOOP

formating:
    ; 将 R3, R2, R1, R0 格式化

    CJNE R0, #10, fmt_ret
    MOV R0, #0
    INC R1
    
    CJNE R1, #10, fmt_ret
    MOV R1, #0
    INC R2

    CJNE R2, #10, fmt_ret
    MOV R2, #0
    INC R3

    CJNE R3, #10, fmt_ret
    MOV R3, #0
    
    fmt_ret:

    RET

printing:
    ; 将 R3, R2, R1, R0 显示到 LED 屏
    ; 改动寄存器 A, P2, P1
    ; 只读寄存器 DPTR, R3, R2, R1, R0

    MOV P2, #0          ; 清除内容
    MOV A, R0           
    MOVC A, @A + DPTR
    MOV P1, A
    MOV P2, #80H        ; 显示到第 1 位

    MOV P2, #0
    MOV A, R1
    MOVC A, @A + DPTR
    ORL A, #80H         ; 显示小数点
    MOV P1, A
    MOV P2, #40H
    
    MOV P2, #0
    MOV A, R2
    MOVC A, @A + DPTR
    MOV P1, A
    MOV P2, #20H
    
    MOV P2, #0
    MOV A, R3
    MOVC A, @A + DPTR
    MOV P1, A
    MOV P2, #10H

    RET

SEGTABLE:
    DB 0C0H ; 0
    DB 0F9H ; 1
    DB 0A4H ; 2
    DB 0B0H ; 3
    DB 99H  ; 4
    DB 92H  ; 5
    DB 82H  ; 6
    DB 0F8H ; 7
    DB 80H  ; 8
    DB 90H  ; 9
    DB 0C7H ; L
    DB 89H  ; H

    END